package cn.kimming.system.controller;

import cn.kimming.common.dto.DefaultPriceDTO;
import cn.kimming.common.dto.RoomTypeAddDTO;
import cn.kimming.common.dto.RoomTypeUpdateDTO;
import cn.kimming.common.pojo.RoomType;
import cn.kimming.common.util.Result;
import cn.kimming.common.vo.DefaultPriceVO;
import cn.kimming.service.IRoomTypeService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 刘铭轩
 * date 2021-05-23
 */
@RestController
@RequestMapping("/roomtype")
public class RoomTypeController {
    @Autowired
    private IRoomTypeService roomTypeService;
    @GetMapping
    public Result findAll() {
        List<RoomType> list = roomTypeService.findAll();
        return new Result(true, "查询成功").addData("list", list);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) {
        RoomType roomType = roomTypeService.findById(id);
        return new Result(true, "查询成功").addData("roomType", roomType);
    }
    @GetMapping("/listDefaultPrice")
    public Result listDefaultPrice() {
        List<DefaultPriceVO> list = roomTypeService.listDefaultPrice();
        return new Result(true, "查询成功").addData("list", list);
    }

    @GetMapping("/findOptions")
    public Result findOptions() {
        List<RoomType> list = roomTypeService.findOptions();
        return new Result(true, "查询成功").addData("list", list);
    }

    @PostMapping
    public Result save(@Valid @RequestBody RoomTypeAddDTO dto) {
        RoomType bo = new RoomType();
        BeanUtils.copyProperties(dto, bo);
        roomTypeService.save(bo);
        return new Result(true, "新增成功");
    }

    @PutMapping
    public Result update(@Valid @RequestBody RoomTypeUpdateDTO dto) {
        RoomType bo = new RoomType();
        BeanUtils.copyProperties(dto, bo);
        roomTypeService.update(bo);
        return new Result(true, "更新成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        roomTypeService.deleteById(id);
        return new Result(true, "删除成功");
    }

    @PutMapping("/updateDefaultPrice")
    public Result updateDefaultPrice(@Valid @RequestBody DefaultPriceDTO dto) {
        RoomType bo = new RoomType();
        BeanUtils.copyProperties(dto, bo);
        roomTypeService.update(bo);
        return new Result(true, "更新成功");
    }

}
